//
// Created by user on 2022/2/28.
//
#include <stack>
#include <string>
using namespace std;
string decodeString(string s) {
    if (s.size()==0)return s;
    stack<string> strStack;
    stack<int> numStack;
    string res;
    int num =0;

    //判断是否是数字
    //遇到【和】之前，判断是否是数字，num拼接， str拼接
    //遇到【， num入栈，str入栈
    //遇到】，去除numstack的数字，复制，取出strStack的字符拼接在前面

    for (char c:s){
        if(isdigit(c)){
            num = num*10+(c-'0');
        } else if(c=='['){
            numStack.push(num);
            num = 0;
            strStack.push(res);
            res="";
        }else if(c==']'){
            int currNum = numStack.top();
            string item = res;
            string currStr = strStack.top();
            for (int i = 1; i < currNum; ++i) {
                res+=item;
            }
            res = currStr+res;
            numStack.pop();
            strStack.pop();
        }else{
            res+=c;
        }
    }
    return res;
}

int main(){

    decodeString("3[a]2[bc]");
    return 0;
}